//
//  YMMobileLoginView.m
//  YMMedical
//
//  Created by y s on 2023/7/13.
//

#import "YMMobileLoginView.h"

#import "YMLoginInputBoxView.h"
#import  "YMLoginService.h"


@interface YMMobileLoginView ()<YMLoginInputBoxViewDelegate>

/** 手机号 */
@property (nonatomic, strong) YMLoginInputBoxView *vMobile;
/** 验证码 */
@property (nonatomic, strong) YMLoginInputBoxView *vCode;

@property (nonatomic, copy) NSString *strMobile;
@property (nonatomic, copy) NSString *strCode;

@end

@implementation YMMobileLoginView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    [self addSubview:self.vMobile];
    [self addSubview:self.vCode];
    
    [self.vMobile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.height.mas_equalTo([SFitUtil fit:50]);
        make.top.mas_equalTo(0);
    }];
    [self.vCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(self.vMobile);
        make.top.equalTo(self.vMobile.mas_bottom).offset([SFitUtil fit:20]);
    }];

}

+ (CGFloat)getViewHeight{
    return [SFitUtil fit:50]*2+[SFitUtil fit:20];
}

#pragma mark -- event action
- (void)loginClickAction{
    if (self.strMobile == nil || _strMobile.length < 1) {
        [SHUD showInfo:KLanguage(@"input_phone_txt")];
        return;
    }
    if (self.strMobile.length < 11) {
        [SHUD showInfo:KLanguage(@"input_right_phone_txt")];
        return;
    }
    if (self.strCode == nil || self.strCode.length < 1) {
        [SHUD showInfo:KLanguage(@"input_sms_code_txt")];
        return;
    }
    if (self.strCode.length < SMS_number_key) {
        [SHUD showInfo:KLanguage(@"input_right_sms_code_txt")];
        return;
    }
    if (self.isAgree == NO) {
        [SHUD showInfo:KLanguage(@"read_agree_ag_txt")];
        return;
    }
    WEAK_SELF;
    [YMLoginService requestUserSmsLoginWithPhone:self.strMobile smsCode:self.strCode success:^{
        
        [weakSelf.viewController.navigationController dismissViewControllerAnimated:YES completion:nil];
    }failure:^(NSString * _Nonnull message) {
        
        [SHUD showInfo:message];
    }];

}
#pragma mark -- YSLoginInputBoxViewDelegate
- (void)textFiledChanged:(UITextField *)sender{
    if (sender.tag == 200) {
        if (sender.text.length > 11) {
            sender.text = [sender.text substringToIndex:11];
        }
        self.strMobile = sender.text;
    }else if (sender.tag == 201){
        if (sender.text.length > SMS_number_key) {
            sender.text = [sender.text substringToIndex:SMS_number_key];
        }
        self.strCode = sender.text;
    }
    [self updateButttonStatus];
}
- (void)getMoboleCode:(nonnull UIButton *)sender {
    //获取验证码
    if (self.strMobile == nil || _strMobile.length < 1) {
        [SHUD showInfo:KLanguage(@"input_phone_txt")];
        return;
    }
    if (self.strMobile.length < 11) {
        [SHUD showInfo:KLanguage(@"input_right_phone_txt")];
        return;
    }
    //获取验证码
    sender.userInteractionEnabled = NO;
    WEAK_SELF;
    [YMLoginService requestUserSendCodeWithPhone:self.strMobile success:^{
        [weakSelf startTimer:sender];
    } failure:^(NSString * _Nonnull message) {
        [SHUD showInfo:message];
        sender.userInteractionEnabled = YES;
    }];
}

- (void)startTimer:(UIButton *)sender{
    [sender setTitleColor:STheme.D4 forState:UIControlStateNormal];
    __block NSInteger allSecond = 60;
    [STimeUtil startTimer:@"GetCodeTimer_Key" second:1 action:^{
        allSecond -= 1;
        [sender setTitle:[NSString stringWithFormat:@"%ldS %@",allSecond,KLanguage(@"resend_format_txt")] forState:UIControlStateNormal];
        if (allSecond == 0) {
            [STimeUtil stopTimer:@"GetCodeTimer_Key"];
            sender.userInteractionEnabled = YES;
            [sender setTitle:KLanguage(@"resend_format_txt") forState:UIControlStateNormal];
            [sender setTitleColor:STheme.D12 forState:UIControlStateNormal];
        }
    }];
}

- (void)updateButttonStatus{
    BOOL isCan = NO;
    if (self.strMobile != nil && _strMobile.length == 11 &&
        self.strCode != nil && self.strCode.length == SMS_number_key) {
        isCan = YES;
    }
    if (self.changeButtonStautsBlock){
        self.changeButtonStautsBlock(isCan);
    }
}

#pragma mark -- setter and getter
- (YMLoginInputBoxView *)vMobile{
    if (_vMobile == nil) {
        _vMobile = [[YMLoginInputBoxView alloc] init];
        _vMobile.strTitle = KLanguage(@"phone_txt");
        _vMobile.strPlace = KLanguage(@"input_txt");
        _vMobile.delegate = self;
        _vMobile.tfTag = 200;
        _vMobile.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _vMobile;
}
- (YMLoginInputBoxView *)vCode{
    if (_vCode == nil) {
        _vCode = [[YMLoginInputBoxView alloc] init];
        _vCode.strTitle = KLanguage(@"sms_code_txt");
        _vCode.strPlace = KLanguage(@"input_txt");
        _vCode.delegate = self;
        _vCode.tfTag = 201;
        _vCode.keyboardType = UIKeyboardTypeNumberPad;
        _vCode.showBtn = YES;
        _vCode.strBtn = KLanguage(@"get_code_txt");
        _vCode.tfAlignment = NSTextAlignmentLeft;
    }
    return _vCode;
}

- (void)dealloc{
    [STimeUtil stopTimer:@"GetCodeTimer_Key"];
}

@end
